package com.genITeam.ria.dao;

import java.util.ArrayList;
import java.util.Set;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;

import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.PersonalMessage;
import com.genITeam.ria.vo.ResponseVo;

public class PersonalMessageDAO {

	public PersonalMessageDAO() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(PersonalMessageDAO.class);

	/**
	 * getPersonalMessage Used to get Personal Messages data
	 * 
	 * @param Member,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public ResponseVo getPersonalMessage(Member member, Session session)
			throws NGFException {

		Member memberMessage = new Member();
		ResponseVo responseVo = new ResponseVo();
		ArrayList personalMessageList = new ArrayList();
		try {
			logger.info("Start getPersonalMessage");
			memberMessage = (Member) session.createCriteria(Member.class).add(
					Expression.eq("id", new Integer(member.getId())))
					.uniqueResult();
			Set messageList = memberMessage.getPersonalMessagesForReceiverId();

			personalMessageList.addAll(messageList);
			responseVo.setPersonalMessageList(personalMessageList);

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}

		return responseVo;
	}

	/**
	 * getReceiverIdByName Get the receiver Id by names
	 * 
	 * @param Member,
	 *            Session
	 * 
	 * @return ResponseVo
	 * @throws NGFException
	 */

	public Member getReceiverIdByName(String receiverName, Session session)
			throws NGFException {

		Member member = null;
		try {
			logger.info("Start getReceiverIdByName");
			member = (Member) session.createCriteria(Member.class).add(
					Expression.eq("userName", receiverName)).uniqueResult();

			if (member == null) {
				throw new NGFException("Recepient \"" + receiverName
						+ "\" doesn't exist.");
			}

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}
		return member;
	}

	/**
	 * savePersonalMessage Save teh new Personal Message nad send to the
	 * Recepient Message List
	 * 
	 * @param PersonalMessage,
	 *            Session
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void savePersonalMessage(PersonalMessage personalMessage,
			Session session) throws NGFException {

		try {
			logger.info("Start savePersonalMessage");
			session.saveOrUpdate(personalMessage);

		} catch (Exception e) {
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}
	}

	/**
	 * deletePersonalMessage Delete the selected Personal Message
	 * 
	 * @param PersonalMessage,
	 *            Session
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void deletePersonalMessage(PersonalMessage personalMessage,
			Session session) throws NGFException {

		try {
			logger.info("Start deletePersonalMessage");
			session.delete(personalMessage);

		} catch (Exception e) {
			logger.error(e.toString());
			throw new NGFException(e.getMessage());
		}
	}

	public static void main(String[] args) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			PersonalMessageDAO personalMessageDAO = new PersonalMessageDAO();
			Member member = new Member();
			member.setId(1);
			personalMessageDAO.getPersonalMessage(member, session);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		}
	}

}
